class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        // 排序
        sort(intervals.begin(), intervals.end());
        vector<vector<int>> ans;
        vector<int> tmp;

        tmp.push_back(intervals[0][0]);
        tmp.push_back(intervals[0][1]);

        for(int i = 1; i < intervals.size(); i++)
        {
            if(intervals[i][0] <= tmp[1])
            {
                tmp[0] = min(tmp[0], intervals[i][0]);
                tmp[1] = max(tmp[1], intervals[i][1]);
            }
            else
            {
                ans.push_back(tmp);
                tmp[0] = intervals[i][0];
                tmp[1] = intervals[i][1];
            }
        }
        ans.push_back(tmp);

        return ans;
    }
};